package com.itheim;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.junit.jupiter.api.Test;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class jwttest {
    @Test
    public void JwtGenerateTest(){
        Map<String, Object> datamap=new HashMap<>();
        datamap.put("id",1);
        datamap.put("username","admin");

        String jwt = Jwts.builder().signWith(SignatureAlgorithm.HS256, "aXRoZWltYQ==")
                .addClaims(datamap)
                .setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60))
                .compact();
        System.out.println(jwt);
    }
    @Test
    public void JwtParseTest(){
        String token = "eyJhbGciOiJIUzI1NiJ9.eyJpZCI6MSwidXNlcm5hbWUiOiJhZG1pbiIsImV4cCI6MTc0Mzg0NDA4Nn0.2pgyjYmnsmgClnWmhLBmqQCY7cQBNSc_3KShjVGfO2A";

        Claims body = Jwts.parser().setSigningKey("aXRoZWltYQ==")
                .parseClaimsJws(token)
                .getBody();
        System.out.println(body);
    }
}
